using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FinanceStats.Entities;

namespace FinanceStats.Business.Components
{
    public partial class OutcomeMonthConotrol : UserControl
    {
        public event OutcomeDayDblClickedHandler OutcomeDayDblClicked;

        private List<OutcomeDay> outcomeMonth;

        private List<Label> panelLabels;

        public List<OutcomeDay> OutcomeMonth
        {
            get { return outcomeMonth; }
            set
            {
                outcomeMonth = value;
                ShowMonthly();
            }
        }

        
        public OutcomeMonthConotrol()
        {
            InitializeComponent();
            InitializePanels();
        }

        public OutcomeMonthConotrol(List<OutcomeDay> month)
        {
            InitializeComponent();
            InitializePanels();
            this.outcomeMonth = month;
            ShowMonthly();
        }

        private void ShowMonthly()
        {
            string textTemplate = "Total \r\n {0}";
            decimal total = 0;
            for (int i = 0; i < outcomeMonth.Count; i++)
            {
                OutcomeDay day = outcomeMonth[i];
                Label lbl = panelLabels[i];
                lbl.Text = String.Format(textTemplate, day.Total.ToString());
                total += day.Total;
            }
            label1.Text = String.Format("Monthly overview for {0} month", outcomeMonth[0].Date.Month);
            label2.Text = String.Format("Total : {0}", total);
            for (int i = outcomeMonth.Count; i < 32; i++)
            {
                panelLabels[i].Text = "";
            }
        }

        private void InitializePanels()
        {
            Font f = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            
            this.panelLabels = new List<Label>();
            this.tableLayoutPanel1.Controls.Clear();
            
            for (int i = 0; i < 32; i++)
            {
                Panel pnl = new Panel();
                pnl.Dock = DockStyle.Fill;
                pnl.Padding = new Padding(5);
                Label lbl = new Label();
                lbl.Name = "" + i;
                lbl.Location = new Point(5, 5);
                lbl.Dock = DockStyle.Fill;
                lbl.AutoSize = false;
                lbl.Font = f;
                lbl.Name = "" + i;
                lbl.BackColor = System.Drawing.Color.White;
                lbl.ForeColor = System.Drawing.SystemColors.MenuHighlight;
                lbl.MouseEnter += new EventHandler(lbl_MouseEnter);
                lbl.MouseLeave += new EventHandler(lbl_MouseLeave);
                lbl.MouseDoubleClick += new MouseEventHandler(lbl_MouseDoubleClick);
                lbl.MouseEnter += new EventHandler(lbl_MouseEnter);
                lbl.MouseLeave += new EventHandler(lbl_MouseLeave);
                lbl.MouseDoubleClick += new MouseEventHandler(lbl_MouseDoubleClick);
                pnl.Controls.Add(lbl);
                pnl.Controls.Add(lbl);
                int row = i / 7;
                int column = i % 7;
                this.tableLayoutPanel1.Controls.Add(pnl, column, row);
                this.panelLabels.Add(lbl);
            }
        }

        private void lbl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.OutcomeDayDblClicked != null)
            {
                string name = ((Label)sender).Name;
                int index = Convert.ToInt32(name);
                OutcomeDay day = outcomeMonth[index];
                this.OutcomeDayDblClicked(day);
            }
        }

        private void lbl_MouseLeave(object sender, EventArgs e)
        {
            Panel p = ((Label)sender).Parent as Panel;
            p.BackColor = System.Drawing.Color.White;
        }

        private void lbl_MouseEnter(object sender, EventArgs e)
        {
            Panel p = ((Label)sender).Parent as Panel;
            p.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
        }
    }
}
